package org.kymjs.kjframe.demo;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.Surface;
import android.view.WindowManager;

public final class ScreenManager {

    private static int deviceScreenHeight = 0;
    private static int deviceScreenWidth = 0;
    private static boolean initialized = false;
    private static int deviceScreenDensity = 0;
    private static Context context = null;

    @SuppressLint("StaticFieldLeak")
    public static void init(Context ctx) {
        if (initialized || ctx == null) return;
        context = ctx;
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics metrics = new DisplayMetrics();
        if (windowManager != null && windowManager.getDefaultDisplay() != null) {
            windowManager.getDefaultDisplay().getRealMetrics(metrics);
        }

        deviceScreenHeight = metrics.heightPixels != 0 ? metrics.heightPixels : context.getResources().getDisplayMetrics().heightPixels;
        deviceScreenWidth = metrics.widthPixels != 0 ? metrics.widthPixels : context.getResources().getDisplayMetrics().widthPixels;

        if (deviceScreenWidth > deviceScreenHeight) {
            int temp = deviceScreenWidth;
            deviceScreenWidth = deviceScreenHeight;
            deviceScreenHeight = temp;
        }

        deviceScreenDensity = metrics.densityDpi;
        initialized = true;
    }

    public static int getDeviceScreenHeight() {
        return deviceScreenHeight;
    }

    public static int getDeviceScreenWidth() {
        return deviceScreenWidth;
    }

    public static int getDeviceScreenDensity() {
        return deviceScreenDensity;
    }

    public static int getAutoScreenWidth() {
        return isLandscape() ? getDeviceScreenHeight() : getDeviceScreenWidth();
    }

    public static int getAutoScreenHeight() {
        return isLandscape() ? getDeviceScreenWidth() : getDeviceScreenHeight();
    }

    private static int scale(int value, int dimension) {
        return (dimension == 0 || !initialized) ? value : value * deviceScreenWidth / dimension;
    }

    public static int scaleX(int x, int width) {
        return scale(x, width);
    }

    public static int scaleY(int y, int height) {
        return scale(y, height);
    }

    private static int rescale(int value, int dimension) {
        return (dimension == 0 || !initialized) ? value : value * dimension / deviceScreenWidth;
    }

    public static int rescaleX(int x, int width) {
        return rescale(x, width);
    }

    public static int rescaleY(int y, int height) {
        return rescale(y, height);
    }

    public static boolean isLandscape() {
        if (context == null) return false;
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        if (windowManager == null || windowManager.getDefaultDisplay() == null) return false;
        int rotation = windowManager.getDefaultDisplay().getRotation();
        return rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270;
    }
}

